package com.veketor;

import java.awt.image.BufferedImage;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

public class Product {
    private long id;
    private String name;
    private double price;
    private int stock;
    private int alarmStock;
    private String imgPath;
    private String date;
    private double tax;
    private int location;
    
    //private static String nameDBTable="PRODUCTS";
    BufferedImage img = null; 
    String stSql = "";
    String className = "Product";
	//private String stType;
    
    //constructor 1.
    public Product(String name, double price, int stock, int alarmStock, String imgPath, double tax, int location)
    {
    	try
    	{
    		this.name=name;
        	this.price=price;
        	this.stock=stock;
        	this.alarmStock=alarmStock;
        	this.imgPath=imgPath;
        	this.date="";
            this.tax=tax;
            this.location=location;        
    	}
    	catch(Exception e)
    	{
    		Debug.out(className+" Product: "+e);
    	}
    }
    
    public Product(String name, double price)
    {
    	this(name, price, -1,-1,"/null.png", -1.0, 0);
    }
    
    public Product()
    {
    	this("", 0.0, 0, 0, "", 0, 0);
    }
    public Product loadFromResulSet(ResultSet rs)
    {
    	int debugLevel=10;
    	try
    	{
    		Debug.out(debugLevel,"product.loadFromResulSet INI");
    		id=Integer.parseInt(rs.getString("id"));
    		Debug.out(debugLevel,"product.loadFromResulSet ID: "+id);
   	 		name=rs.getString("name");
   	 		Debug.out(debugLevel,"product.loadFromResulSet name: "+name);
   	 		if (rs.getString("price")!=null)
   	 		{
   	 			price=Double.parseDouble(rs.getString("price"));	
   	 		}
   	 		else{	price=0;	}
   	 		Debug.out(debugLevel,"product.loadFromResulSet price: "+price);
   	 		if (rs.getString("stock")!=null)
   	 		{
   	 			stock=Integer.parseInt(rs.getString("stock"));
   	 		}
   	 		else{	stock = -1;	}
   	 		Debug.out(debugLevel,"product.loadFromResulSet stock: "+stock);
   	 		if (rs.getString("alarmStock")!=null)
   	 		{
   	 			alarmStock=Integer.parseInt(rs.getString("alarmStock"));
   	 		} else{alarmStock=-1;}
   	 		Debug.out(debugLevel,"product.loadFromResulSet alarmStock: "+alarmStock);
   	 		imgPath=rs.getString("imgPath");
   	 		Debug.out(debugLevel,"product.loadFromResulSet imgPath: "+imgPath);
   	 		date=rs.getString("date");
   	 		Debug.out(debugLevel,"product.loadFromResulSet date: "+date);
   	 		if ((rs.getString("tax")!=null))
   	 		{
   	 			tax=Double.parseDouble(rs.getString("tax"));	
   	 		}
   	 		else{tax=0.0;}
   	 		Debug.out(debugLevel,"product.loadFromResulSet tax: "+tax);
   	 		if ((rs.getString("location")!=null))
   	 		{
   	 			location=Integer.parseInt(rs.getString("location"));
   	 		}
   	 		else{location=0;}
   	 		Debug.out(debugLevel,"product.loadFromResulSet location: "+location);
    	}
    	catch (SQLException e)	{	Debug.out(e);  }
    	catch (Exception e)	{	Debug.out(e);  }
    	return this;
    }
    
    public void buy(int numberOfProducts)
    {
    	stock=stock-numberOfProducts;
    }
    
    public void toDataBase()
    {
    	try
    	{
    		if (this.isInDataBase()==false)
    		{
    			DbManager.updTable("Insert into PRODUCTS Values (NULL,'"+this.name+"',"+this.price+","+stock+","+alarmStock+",'"+this.imgPath+" ',date('now'),"+this.tax+","+this.location+")");
    		}
    		else
    		{
    			Debug.out(1,className+"toDataBase ERROR: There is a product whit same name in database ["+this.name+"]");
    		}
    	}
    	catch(Exception e)
    	{
    		Debug.out(e);
    	}
    }
    
    public boolean isInDataBase()
    {
    	boolean result = false;
    	try
    	{
    		ResultSet rs;    	
    		int howMuch=0;
    		rs = DbManager.query("Select count (*) as number from PRODUCTS where name= '"+this.name+"'");
    		howMuch=rs.getInt("number");
    		if (howMuch>0)
    		{
    			result=true;
    		}
    		return result;
    	}
    	catch(Exception e)
    	{
    		Debug.out(e);
    		return result;
    	}
    }
    
    public  long getIdFromDataBase()
    {
    	long dbId=-1;
    	try
    	{
    		ResultSet rs;    	   		
	    		rs = DbManager.query("Select id from PRODUCTS where name= '"+this.name+"'");
	    		dbId=rs.getInt("id");
	    	
    	}
    	catch (Exception e)
    	{
    		Debug.out(e);
    	}
    	return dbId;
    }
    //This methods is use to send data to a SQL query
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public Vector toVector()
    {
    	Vector tempVector = new Vector(9);
    	try
    	{
    		tempVector.add(id);
    		tempVector.add(name);
    		tempVector.add(price);
    		tempVector.add(stock);
    		tempVector.add(alarmStock);
    		tempVector.add(imgPath);
    		tempVector.add(date);
    		tempVector.add(tax);
    		tempVector.add(location);
    	}
    	catch(Exception e){Debug.out(e);}
    	return tempVector;
    }
    
    public long getId() {
        return id;
    }

    public Product getProduct()
    {
    	return this;
    }

    public String getNombre() {
        return name;
    }
 
    public void setNombre(String name) {
        this.name = name;
    }
 
    public double getUnitPrice() {
        return price;
    }
 
    public void setPrecioUnitario(double unitPrice) {
        this.price = unitPrice;
    }
 
    public int getStock() {
        return stock;
    }
 
    public void setStock(int stock) 
    {
        this.stock = stock;
    }
    
    public Boolean isAlarm()
    {//TODO change name of this method
    	return this.alarmStock<=this.stock;
    }
 
    @Override
    public String toString() {
    	return toStringDB(false);
    }
    
    protected String toStringDB(boolean isDB) {
    	String stTemp ="";
    	@SuppressWarnings("rawtypes")
		Vector tempVector = this.toVector();
    	try{
    		int beginInPos=0;
    		if (isDB)
    		{
    			beginInPos=1;
    			stTemp="'NULL', ";
    		}
    		
    		for (int i=beginInPos; i<tempVector.size()-1; i++)
    		{
    			String stType = tempVector.elementAt(i).getClass().getName();
    			boolean isString = stType.equals("java.lang.String"); 
    			//boolean isFixed = (/*stType.equals("java.lang.Long") || stType.equals("java.lang.Double")||*/stType.equals("java.lang.Float"));
    			
    			if (isString){stTemp=stTemp+"'";}
    			
    			stTemp=stTemp+tempVector.elementAt(i);
    			if (isString){stTemp=stTemp+"'";}
    			
    			stTemp=stTemp+",";
    		}
    		stTemp=stTemp+tempVector.elementAt(tempVector.size()-1);
    	}
    	catch(Exception e){ Debug.out(e);	}
    return stTemp;
    }
}